package com.example.poverty.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.poverty.entity.VillagersMessage;
import com.example.poverty.service.VillagersMessageService;
import com.example.poverty.utils.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 前端 村民留言 控制类
 */
@RestController
@RequestMapping("/villagersMessage")
public class VillagersMessageController {

    @Autowired
    private VillagersMessageService villagersMessageService;

    /**
     * 添加村民留言
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Object addVillagersMessage(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        String villageName = request.getParameter("villageName").trim();  //村民姓名
        String townName = request.getParameter("townName").trim();  //村镇姓名
        String cadreName = request.getParameter("cadreName").trim();  //干部姓名
        String time = request.getParameter("time").trim();  //留言时间
        String status = request.getParameter("status").trim();  //留言状态
        String messageContent = request.getParameter("messageContent").trim();  //留言内容
        String resolutionStatus = request.getParameter("resolutionStatus").trim();  //解决状态
        String typeOfDifficulty = request.getParameter("typeOfDifficulty").trim();  //困难类型

        // 把留言时间转成Date格式
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date timeDate = new Date();
        try {
            timeDate = dateFormat.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        VillagersMessage villagersMessage = new VillagersMessage();
        villagersMessage.setVillageName(villageName);
        villagersMessage.setTownName(townName);
        villagersMessage.setCadreName(cadreName);
        villagersMessage.setTime(timeDate);
        villagersMessage.setStatus(new Byte(status));
        villagersMessage.setMessageContent(messageContent);
        villagersMessage.setResolutionStatus(new Byte(resolutionStatus));
        villagersMessage.setTypeOfDifficulty(typeOfDifficulty);
        boolean flag = villagersMessageService.insert(villagersMessage);
        if (flag) {
            jsonObject.put(Const.CODE, 1);
            jsonObject.put(Const.MSG, "添加成功");
            return jsonObject;
        }
        jsonObject.put(Const.CODE, 0);
        jsonObject.put(Const.MSG, "添加失败");
        return jsonObject;
    }

    /**
     * 修改 村民留言
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Object updateVillagersMessage(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        String id = request.getParameter("id").trim();  //主键
        String villageName = request.getParameter("villageName").trim();  //村民姓名
        String townName = request.getParameter("townName").trim();  //村镇姓名
        String cadreName = request.getParameter("cadreName").trim();  //干部姓名
        String time = request.getParameter("time").trim();  //留言时间
        String status = request.getParameter("status").trim();  //留言状态
        String messageContent = request.getParameter("messageContent").trim();  //留言内容
        String resolutionStatus = request.getParameter("resolutionStatus").trim();  //解决状态
        String typeOfDifficulty = request.getParameter("typeOfDifficulty").trim();  //困难类型


        // 把留言时间转成Date格式
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date timeDate = new Date();
        try {
            timeDate = dateFormat.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        VillagersMessage villagersMessage = new VillagersMessage();
        villagersMessage.setId(Integer.parseInt(id));
        villagersMessage.setVillageName(villageName);
        villagersMessage.setTownName(townName);
        villagersMessage.setCadreName(cadreName);
        villagersMessage.setTime(timeDate);
        villagersMessage.setStatus(new Byte(status));
        villagersMessage.setMessageContent(messageContent);
        villagersMessage.setResolutionStatus(new Byte(resolutionStatus));
        villagersMessage.setTypeOfDifficulty(typeOfDifficulty);
        boolean flag = villagersMessageService.update(villagersMessage);
        if (flag) {
            jsonObject.put(Const.CODE, 1);
            jsonObject.put(Const.MSG, "修改成功");
            return jsonObject;
        }
        jsonObject.put(Const.CODE, 0);
        jsonObject.put(Const.MSG, "修改失败");
        return jsonObject;
    }

    /**
     * 删除村民留言
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public Object deleteVillagersMessage(HttpServletRequest request) {
        String id = request.getParameter("id").trim();  //主键
        boolean flag = villagersMessageService.delete(Integer.parseInt(id));
        return flag;
    }

    /**
     * 根据主键查询村民留言
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/selectByPrimaryKey", method = RequestMethod.GET)
    public Object selectByPrimaryKey(HttpServletRequest request) {
        String id = request.getParameter("id").trim();  //主键
        return villagersMessageService.delete(Integer.parseInt(id));
    }

    /**
     * 查询所有的留言信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/allVillagersMessage", method = RequestMethod.GET)
    public Object allVillagersMessage(HttpServletRequest request) {
        return villagersMessageService.allVillagersMessage();
    }

    /**
     * 根据村名姓名查询留言信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/getByName", method = RequestMethod.GET)
    public Object getByName(HttpServletRequest request) {
        String villageName = request.getParameter("villageName").trim();  //村民姓名
        return villagersMessageService.getByName(villageName);
    }


}
